﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Configuration;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml;

namespace teavch.Util.RssFeedator
{
	public class Feedator
	{
		public static Hashtable GetFeedatorSource()
		{
			Hashtable hash = new Hashtable();
			string asKey = "FeedatorSource000";
			string asValue = ConfigurationManager.AppSettings[ asKey ];
			int iSourceCount = 0;
			if ( int.TryParse( asValue, out iSourceCount ) )
			{
				for ( int i = 1; i <= iSourceCount; i++ )
				{
					asKey = "FeedatorSource" + i.ToString().PadLeft( 3, '0' );
					asValue = ConfigurationManager.AppSettings[ asKey ];
					hash.Add( asKey, asValue );
				}
			}
			return hash;
		}

		public static List<Rss> CreateRss()
		{
			List<Rss> allRSS = new List<Rss>();

			Hashtable hash = Feedator.GetFeedatorSource();
			foreach ( string o in hash.Values )
			{
				Rss rss = Feedator.GetObject( new Uri( o ) );
				allRSS.Add( rss );
			}
			return allRSS;
		}

		public static Rss CreateRss( Uri FeedUri )
		{
			return Feedator.GetObject( FeedUri );
		}

		private static Rss GetObject( Uri FeedUri )
		{
			Rss rss = null;
			try
			{
				HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( FeedUri );
				HttpWebResponse response = ( HttpWebResponse ) request.GetResponse();

				if ( response.StatusCode == HttpStatusCode.OK )
				{
					Stream receiveStream = response.GetResponseStream();
					StreamReader readStream = null;
					if ( response.CharacterSet == null )
						readStream = new StreamReader( receiveStream );
					else
						readStream = new StreamReader( receiveStream, Encoding.GetEncoding( response.CharacterSet ) );
					string data = readStream.ReadToEnd();

					XmlDocument doc = new XmlDocument();
					doc.LoadXml( data );

					XmlNodeList xmlChannels = doc.GetElementsByTagName( "channel" );

					rss = new Rss();
					ChannelContent cc = null;

					if ( xmlChannels.Count == 1 )
					{
						foreach ( XmlNode channelValue in xmlChannels[ 0 ].ChildNodes )
						{

							if ( channelValue.NodeType == XmlNodeType.Element )
							{
								if ( channelValue.Name == rss.Channel.Title.Tag )
									rss.Channel.Title.Text = channelValue.InnerText;
								if ( channelValue.Name == rss.Channel.Link.Tag )
									rss.Channel.Link.Text = channelValue.InnerText;
								if ( channelValue.Name == rss.Channel.Description.Tag )
									rss.Channel.Description.Text = channelValue.InnerText;

								if ( channelValue.Name == "item" )
								{
									if ( channelValue.HasChildNodes )
									{
										if ( channelValue.NodeType == XmlNodeType.Element )
										{
											cc = new ChannelContent();
											foreach ( XmlNode itemChild in channelValue.ChildNodes )
											{
												//
												if ( itemChild.Name == cc.Title.Tag )
													cc.Title.Text = itemChild.InnerText;
												if ( itemChild.Name == cc.Link.Tag )
													cc.Link.Text = itemChild.InnerText;
												if ( itemChild.Name == cc.Description.Tag )
													cc.Description.Text = itemChild.InnerText;
												if ( itemChild.Name == cc.Author.Tag )
													cc.Author.Text = itemChild.InnerText;
												if ( itemChild.Name == cc.PublishDate.Tag )
													cc.PublishDate.Text = itemChild.InnerText;
												if ( itemChild.Name == cc.Guid.Tag )
													cc.Guid.Text = itemChild.InnerText;
											}
											rss.Channel.ChannelContent.Add( cc );
										}

									}

								}
							}
						}
					}

					//xmlChannels = doc.GetElementsByTagName( "item" );

					//foreach ( XmlNode node in xmlChannels[ 0 ].ChildNodes )
					//{
					//	if ( node.HasChildNodes )
					//	{
					//		foreach ( XmlNode item in node.ChildNodes )
					//		{
					//			cc = new ChannelContent();
					//			if ( item.NodeType == XmlNodeType.Element && item.HasChildNodes )
					//			{
					//				foreach ( XmlNode itemChild in item.ChildNodes )
					//				{
					//					if ( itemChild.Name == cc.Title.Tag )
					//						cc.Title.Text = itemChild.InnerText;
					//					if ( node.Name == cc.Link.Tag )
					//						cc.Link.Text = itemChild.InnerText;
					//					if ( node.Name == cc.Description.Tag )
					//						cc.Description.Text = itemChild.InnerText;
					//					if ( node.Name == cc.Author.Tag )
					//						cc.Author.Text = itemChild.InnerText;
					//					if ( node.Name == cc.PublishDate.Tag )
					//						cc.PublishDate.Text = itemChild.InnerText;
					//					if ( node.Name == cc.Guid.Tag )
					//						cc.Guid.Text = itemChild.InnerText;
					//				}
					//			}
					//		}
					//	}
					//}



					response.Close();
					readStream.Close();
				}
				else
				{
					return null;
				}
			}
			catch ( Exception ) { return null; }
			return rss;
		}
	}
}
